/***************************************************************************
 *   Copyright (C) 2004 by cyril dupuit                                    *
 *   cyrildupuit@hotmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

//*****************************************************************************
// Nom du module : Serial.c
// Description : Gestion des ports series
//*****************************************************************************
 
// Historique :
// 28/10/04 : Creation du module V1.0  

#include "Kernel/Core.h"
#include "Arch/Pentium/Kernel/Cpu.h"
#include "Arch/Pentium/Driver/Serial.h"

//*****************************************************************************
// Procedure globales a ce module :
//*****************************************************************************
static unsigned short SerialPort;
static unsigned short  _serial_iobases [] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 } ;

//*****************************************************************************
// But de la fonction : Tester si un caractere se trouve dans le buffer de
// reception
// Entree : Aucune
// Parametre retourne : TRUE si aucun caractere recu, FALSE sinon
//*****************************************************************************
Status_t SerialIsReady(void)
{
	//Test du buffer de reception
	if((InByte(SerialPort + UART_LSR) & UART_DATA_READY) == 0)
	{
  		return TRUE;//Le buffer est vide
	}
	return FALSE;
}

//*****************************************************************************
// But de la fonction : Recevoir un caractere par la ligne serie
// Entree : Aucune
// Parametre retourne : Caractere lu
//*****************************************************************************
char SerialGetchar (void)
{
	char Value;

	do
	{//Attendre tant que le buffer de reception est vide
		Value = InByte(SerialPort | UART_LSR);
	}
	while((Value & UART_DATA_READY) == 0);

	//Lire et retourner la valeur recu
	return InByte (SerialPort | UART_RX);
}

//*****************************************************************************
// But de la procedure : Emettre un caractere sur la ligne serie
// Entree :
//	C : Caractere a envoyer sur la ligne
// Sortie : Aucune
//*****************************************************************************
void SerialPutchar(char C)
{
	//Attendre que le registre d'emission soit vide avant d'emettre
	while((InByte(SerialPort | UART_LSR) & UART_EMPTY_TRANSMITTER) == 0);

	OutByte(SerialPort | UART_TX, C);
}

//*****************************************************************************
// But de la fonction : Initialiser le port serie
// Entrees :
//	Unit : Numero du port a selectionner
//	Speed : Debit en bauds
//	WordLen : Taille d'un paquet de donne
//	Parity : Parite paire, impaire ou aucune
//	StopBitLen : Nombre de bits de stop (1, 1.5, 2)
// Parametre retourne : ERROR si le numero de port est incorrecte ou si le 
//debit est superieur a SERIAL_BAUDRATE_MAX, TRUE sinon
//*****************************************************************************
Status_t SerialInit(unsigned short Unit, unsigned long Speed, int WordLen, int Parity, int StopBitLen)
{
	unsigned short Div;
	unsigned char Status;

	if (Unit >= 4) return ERROR;
  
	SerialPort = _serial_iobases[Unit];
  
	/* Turn off the interrupt.  */
	OutByte(SerialPort + UART_IER, 0);

	/* Set DLAB.  */
	OutByte(SerialPort + UART_LCR, UART_DLAB);
  
	/* Set the baud rate.  */
	if(Speed > SERIAL_BAUDRATE_MAX) return ERROR;

	Div = (unsigned short)((unsigned long)(SERIAL_BAUDRATE_MAX / Speed));
  
	OutByte(SerialPort + UART_DLL, Div & 0xFF);
	OutByte(SerialPort + UART_DLH, Div >> 8);
  
	/* Set the line status.  */
	Status = Parity | WordLen | StopBitLen;
	OutByte(SerialPort + UART_LCR, Status);

	/* Enable the FIFO.  */
	OutByte(SerialPort + UART_FCR, UART_ENABLE_FIFO);

	/* Turn on DTR, RTS, and OUT2.  */
	OutByte(SerialPort + UART_MCR, UART_ENABLE_MODEM);

	InByte(SerialPort + UART_LSR);/* Clear any pre-existing overrun indication */
	InByte(SerialPort + UART_IIR);	/* Clear any pre-existing transmit interrupt */

	while(SerialIsReady() == FALSE) InByte(SerialPort + UART_RX);/* Vider le buffer de reception  */
  
	return TRUE;
}

//*****************************************************************************
